/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ai;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;

/**
 *
 * @author root
 */
public class MapExp extends LexExp {
    public MapExp(LabelExp pLabelArray[]){
        mLabelArray = pLabelArray;
        mSets = new ArrayList<HashSet<String>>();
        mTable = new HashMap<String, HashMap<String,String> >();
        for(int i=0; i < mLabelArray.length ; i++ ){
            mSets.add(new HashSet<String>());
            for(int j=0; j < mLabelArray.length ; j++ ){
                if( i != j){
                    HashMap<String,String> lHMap = new HashMap<String,String>();
                    mTable.put(getKey(i,j),lHMap);
                }
            }            
        }
    }
    public int findLabel(String pLabel){
        for (int i = 0; i < mLabelArray.length; i++){
            if (mLabelArray[i].getName().equals(pLabel)){
                return i;
            }
        }
        return -1;
    }
    
    public HashMap<String,String> getMap(String pLabel1, String pLabel2){
        return mTable.get(pLabel1 + "<>" + pLabel2);
    }
    
    public boolean hasWord(String pWord, int pIndex){
        Iterator it = mSets.get(pIndex).iterator();
        while(it.hasNext()){
            if(pWord.matches((String)it.next())){
                return true;
            }
        }
        return false; 
    }
    public LabelExp getLabel(int pN){
        return mLabelArray[pN];
    }
    public int getLabelCount(){
        return mLabelArray.length;
    }
    public HashSet<String> getSet(int i){
        return mSets.get(i);
    }
    public void addMapping(String pStrArray[]){
        for(int i=0 ; i < pStrArray.length ; i++){
            //System.out.println("Strings "+pStrArray[i]);
            mSets.get(i).add(pStrArray[i]);
            for(int j=0 ; j < pStrArray.length ; j++){
                if( i != j){
                    mTable.get( getKey(i,j) ).put(pStrArray[i], pStrArray[j]);
                }
            }
        }
        //System.out.println(mSets.toString());
        //System.out.println(mTable.toString());
    }
    private String getKey(int i, int j){
        return mLabelArray[i].getName() + "<>" + mLabelArray[j].getName();
    }
    public LexExp substitute(String pLabelName, String pString){
        System.out.println("MapExp.substitute(String pLabelName = " + pLabelName + ", String pString = " + pString + "): Warning! Can't substitute a map!");
        return this;
    }
    private LabelExp mLabelArray[];
    private ArrayList<HashSet<String>> mSets;
    private HashMap<String, HashMap<String,String> > mTable;
}
